<?php

namespace app\backend\controller;
use app\backend\service\ZhMatchmakerLevelInterestService;
use app\backend\model\ZhMatchmakerLevelInterest as ZhMatchmakerLevelIntroduceModel;


class ZhMatchmakerLevelInterest extends Admin
{
    /*首页数据列表*/
    function index()
    {
        if (!$this->request->isAjax()) {
            return view('index');
        } else {
            $limit           = $this->request->post('limit', 20, 'intval');
            $offset          = $this->request->post('offset', 0, 'intval');
            $where['text']   = $this->request->param('text', '', 'serach_in');

            $page = floor($offset / $limit) + 1;
            $res  = ZhMatchmakerLevelInterestService::indexList(formatWhere($where), $limit, $page);
            return json($res);
        }
    }

    public function update(){
        if (!$this->request->isPost()) {
            $id = $this->request->get('id', '', 'serach_in');
            $this->view->assign('info', checkData(ZhMatchmakerLevelIntroduceModel::find($id)));
            return view('update');
        } else {
            $postField = 'title,id,image,sort';
            $data      = $this->request->only(explode(',', $postField), 'post', null);
            $res = ZhMatchmakerLevelIntroduceModel::update($data);
            return json(['status' => '00', 'msg' => '修改成功']);
        }
    }

    /*删除*/
    function delete(){
        $idx =  $this->request->post('id', '', 'serach_in');
        if(!$idx) $this->error('参数错误');
        try{
            ZhMatchmakerLevelIntroduceModel::destroy(['id'=>explode(',',$idx)],true);
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }
        return json(['status'=>'00','msg'=>'操作成功']);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @author  lwx
     */
    function add()
    {
        if (!$this->request->isPost()) {
            return view('add');
        } else {
            $postField          = 'title,id,image,sort';
            $data               = $this->request->only(explode(',', $postField), 'post', null);

            $res                = ZhMatchmakerLevelIntroduceModel::create($data);
            return json(['status' => '00', 'msg' => '添加成功']);
        }
    }
}